package com.example.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.common.util.R;
import com.example.model.article.pojo.ArticleConfig;
import com.example.model.comment.dto.CommentConfigDto;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.article.service.ArticleConfigService;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;

@RestController
@RequestMapping("/api/v1")
@Tag(name = "文章配置管理")
public class ArticleConfigController {
    @Autowired
    private ArticleConfigService articleConfigService;

    @GetMapping(value = "/articleConfig/getArticleConfigByArticleId/{articleId}")
    @Operation(summary = "按照文章编号查看文章配置")
    public R getArticleConfigByArticleId(@PathVariable(value = "articleId") Long articleId) {
        LambdaQueryWrapper<ArticleConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleConfig::getArticleId, articleId);
        ArticleConfig articleConfig = articleConfigService.getOne(wrapper);
        if (articleConfig != null) {
            return R.ok("按照文章编号查看文章配置成功").data("articleConfig", articleConfig);
        } else {
            return R.error("按照文章编号查看文章配置失败");
        }
    }

    @PostMapping(value = "/articleConfig/updateCommentStatus")
    @Operation(summary = "修改评论状态")
    public R updateCommentStatus(@RequestBody CommentConfigDto commentConfigDto) {
        LambdaUpdateWrapper<ArticleConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ArticleConfig::getArticleId, commentConfigDto.getArticleId());
        wrapper.set(ArticleConfig::getIsComment, commentConfigDto.getOperation());
        boolean flag = articleConfigService.update(wrapper);
        if (flag) {
            return R.ok("修改评论状态成功");
        } else {
            return R.error("修改评论状态失败");
        }
    }
}